import apis from '../../api/apis.js';
export default {
    namespaced: true,
    state: {
        goodsData: [],
        seckillData: [],
        findData: [],
        spData: [],
        shopCarts: [], //用户购物车
        orderId: [], //订单Id
        goodsOrder: [], //用户所有订单
        names: ''
    },
    mutations: {
        setname(state, data) {
            state.names = data
            console.log('a', state.names)
        },
        xgdata(state, data) {
            state.goodsData = data

        },
        // lin,设置秒杀数据
        setSeckillData(state, newData) {
            state.seckillData = newData;
        },
        findata(state, data) {
            state.findData = data

        },
        spdata(state, data) {
            state.spData = data

            // console.log(state.spData)
        },
        //购物车
        setShopCarts(state, data) {
            state.shopCarts = data
        },
        // 购物车id
        setOrderId(state, data) {
            state.orderId.push(data);
        },
        //商品订单
        setGoodsOrder(state, data) {
            state.goodsOrder = data;
        }
    },
    actions: {
        async getshop(context) {
            let data = await apis.goods.getShop()
                // console.log(data)
            context.commit('xgdata', data.data)
        },
        // lin，首页底部推荐商品
        async homeGoods() {
            const res = await apis.goods.getHomeGoods();
			console.log("首页商品推荐",res)
            return res;
        },
        // lin, 秒杀数据
        async seckillGoods(context) {
            const res = await apis.goods.getSeckillData();
            if (res.result) {
                context.commit('setSeckillData', res.data);
            }
        },
        //商城首页点击商品类型获取该类型下的商品数据
        async findShop(context, data) {
            const find = await apis.goods.finShop(data)
            context.commit('findata', find.list)
            return find;
        },
        //点击商品进入商品详情的接口
        async finid(context, data) {
            const datas = await apis.goods.findById(data)
            context.commit('spdata', datas.goods)

            return datas
        },
        //点击商品立即购买
        async ljgm(context, data) {
            const gm = await apis.goods.ljzf(data)
            return gm
        },
        //点击收藏商品
        async ljsc(context, data) {
            const sca = await apis.goods.sc(data)

            return sca
        },
        //获取购物车商品
        async userShopCart(context, data) {
            const res = await apis.goods.getShopCart(data);
            context.commit('setShopCarts', res.list)
            return res;
        },
        //购物车立即购买
        async userBuyCarts(context, data) {
            const res = await apis.goods.buyCart(data);
            context.commit("setOrderId", res.id);
            console.log(res.id);
            return res
        },
        //设置购物车商品数量
        async userCartsNum(context, data) {
            const res = await apis.goods.setCartsNum(data);
            context.dispatch("userShopCart");
            return res
        },
        //删除购物车
        async userDelShopCarts(context, data) {
            const res = await apis.goods.delShopCarts(data);
            context.dispatch("userShopCart");
            return res;
        },
        //清空购物车
        async userEmptyCarts(context, data) {
            const res = await apis.goods.emptyCarts(data);
            context.dispatch("userShopCart");
            return res
        },
        //查看商品订单
        async userGoodsOrder(context, data) {
            const res = await apis.goods.lookGoodsOrder(data);
            context.commit("setGoodsOrder", res.list);
        },
        //点击立即支付
        async userNowPay(context, data) {
            const res = await apis.goods.nowPay(data);
            return res
        }
    }
}